package com.stock.demo.framework.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.stock.demo.R;


/**
 * Created by XuZhaoyu on 2017/4/17.
 */

public class EditTextWithDelBtn extends LinearLayout {

    private View mView;
    private LinearLayout mLlContainer;
    private ImageView mLine;
    private EditText mEtContent;
    private FrameLayout mFlRevokeIcon;
    private FrameLayout mFlDelIcon;
    private String mEtInputType;
    private String mEtHint;
    private String mEtText;
    private boolean mShowRevoke = true;
    private int mEtMaxLength;
    private String mPreviousText;
    private OnContentChangeListener mContentChangeListener;
    private int mTextColor = -100;
    private int mHintColor = -100;
    private int mBackgroundColor = -100;

    public EditTextWithDelBtn(Context context) {
        this(context, null);
    }

    public EditTextWithDelBtn(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditTextWithDelBtn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAttrs(attrs);
        setAttrs();
    }

    private void setAttrs() {
        if (!TextUtils.isEmpty(mEtInputType)) {
            switch (mEtInputType) {
                case "0":
                    mEtContent.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    break;
                case "1":
                    mEtContent.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                    break;
                default:
                    mEtContent.setInputType(EditorInfo.TYPE_CLASS_TEXT);
                    break;
            }
        } else {
            mEtContent.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        }
        if (!TextUtils.isEmpty(mEtHint)) {
            mEtContent.setHint(mEtHint);
        }
        if (!TextUtils.isEmpty(mEtText)) {
            mEtContent.setText(mEtText);
        }
        if (mShowRevoke) {
            initRevoke();
        }
        initDel();
        if (mEtMaxLength != 0) {
            mEtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mEtMaxLength)});
        } else {
            mEtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(Integer.MAX_VALUE)});
        }
        if (mTextColor != -100) {
            mEtContent.setTextColor(mTextColor);
        } else {
            mEtContent.setTextColor(getResources().getColor(R.color.colorBlack));
        }
        if (mHintColor != -100) {
            mEtContent.setHintTextColor(mHintColor);
        } else {
            mEtContent.setHintTextColor(getResources().getColor(R.color.colorTextHint));
        }
        if (mBackgroundColor != -100) {
            mLlContainer.setBackgroundColor(mBackgroundColor);
        } else {
            mLlContainer.setBackgroundColor(getResources().getColor(R.color.colorWhite));
        }
    }

    private void initDel() {
        mFlDelIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
    }

    private void delete() {
        mPreviousText = mEtContent.getText().toString().trim();
        mEtContent.setText("");
    }

    private void initRevoke() {
        mFlRevokeIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                revoke();
            }
        });
    }

    private void revoke() {
        if (TextUtils.isEmpty(mEtContent.getText().toString().trim())) {
            if (!TextUtils.isEmpty(mPreviousText)) {
                mEtContent.setText(mPreviousText);
                mEtContent.setSelection(mEtContent.length());
            }
        }
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.edit_with_del_btn);
        mEtInputType = typedArray.getString(R.styleable.edit_with_del_btn_et_with_del_input_type);
        mEtHint = typedArray.getString(R.styleable.edit_with_del_btn_et_with_del_hint);
        mEtText = typedArray.getString(R.styleable.edit_with_del_btn_et_with_del_text);
        mShowRevoke = typedArray.getBoolean(R.styleable.edit_with_del_btn_et_with_del_show_revoke, true);
        mEtMaxLength = typedArray.getInteger(R.styleable.edit_with_del_btn_et_with_del_max_length, Integer.MAX_VALUE);
        mTextColor = typedArray.getColor(R.styleable.edit_with_del_btn_et_with_del_text_color, getResources().getColor(R.color.colorBlack));
        mHintColor = typedArray.getColor(R.styleable.edit_with_del_btn_et_with_del_hint_color, getResources().getColor(R.color.colorTextHint));
        mBackgroundColor = typedArray.getColor(R.styleable.edit_with_del_btn_et_with_del_background_color, getResources().getColor(R.color.colorWhite));
        typedArray.recycle();
    }

    private void initView() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.view_eidt_text_with_del_btn, this, true);
        mLlContainer = (LinearLayout) mView.findViewById(R.id.et_with_del_btn_container);
        mFlDelIcon = (FrameLayout) mView.findViewById(R.id.fl_et_with_del_btn_del_icon);
        mFlRevokeIcon = (FrameLayout) mView.findViewById(R.id.fl_et_with_del_btn_revoke_icon);
        mEtContent = (EditText) mView.findViewById(R.id.et_with_del_btn_content);
        mLine = (ImageView) mView.findViewById(R.id.et_with_del_btn_line);
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (mContentChangeListener != null) {
                    mContentChangeListener.beforeContentChange(s, start, count, after);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mContentChangeListener != null) {
                    mContentChangeListener.OnContentChange(s, start, before, count);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mView.hasFocus()) {
                    showIcon(s.toString());
                }
                if (mContentChangeListener != null) {
                    mContentChangeListener.afterContentChange(s);
                }
            }
        });
        mEtContent.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    showIcon(mEtContent.getText().toString().trim());
                    mLine.setBackgroundResource(R.color.colorPrimary);
                } else {
                    mFlDelIcon.setVisibility(GONE);
                    mFlRevokeIcon.setVisibility(GONE);
                    mLine.setBackgroundResource(R.color.colorLine);
                }
            }
        });
    }

    public void showIcon(String str) {
        if (TextUtils.isEmpty(str)) {
            mFlDelIcon.setVisibility(GONE);
            if (mPreviousText == null) {
                mFlRevokeIcon.setVisibility(GONE);
            } else {
                if (mShowRevoke) {
                    mFlRevokeIcon.setVisibility(VISIBLE);
                }
            }
        } else {
            mFlDelIcon.setVisibility(VISIBLE);
            mFlRevokeIcon.setVisibility(GONE);
        }
    }

    public void setText(String text) {
        mEtContent.setText(text);
        mEtContent.setSelection(mEtContent.length());
    }

    public String getText() {
        return mEtContent.getText().toString().trim();
    }

    public interface OnContentChangeListener {
        void beforeContentChange(CharSequence s, int start, int count, int after);

        void OnContentChange(CharSequence s, int start, int before, int count);

        void afterContentChange(Editable s);
    }

    public void setOnContentChangeListener(OnContentChangeListener listener) {
        mContentChangeListener = listener;
    }

    public void setTextColor(int textColor) {
        mTextColor = textColor;
    }

    public void setHintColor(int hintColor) {
        mHintColor = hintColor;
    }

    public void setBackgroundColor(int backgroundColor) {
        mBackgroundColor = backgroundColor;
    }

    public boolean isFocused() {
        return mEtContent.isFocused();
    }

}
